package org.teamh.gui.ingame;

import org.lwjgl.input.Keyboard;
import org.teamh.gui.GUI;

import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.PopupBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.label.builder.LabelBuilder;
import de.lessvoid.nifty.elements.Element;

public class LeaveGameScreen {

	private GUI gui;
	private Element popup;
	
	private boolean isShowing = false;
	
	public LeaveGameScreen(GUI gui) {
		this.gui = gui;
		
		initPopup();
	}
	
	private void initPopup() {
		new PopupBuilder("leave_game_popup") {{
			backgroundColor("#00000099");
			childLayoutCenter();
			panel(new PanelBuilder() {{
				childLayoutVertical();
				style("nifty-panel");
				width("300px");
				height("200px");
				
				//upper panel for displaying text
				panel(new PanelBuilder() {{
					childLayoutCenter();
					valignCenter();
					height("90px");
					width("260px");
					control(new LabelBuilder() {{
						width("260px");
						text("Do you really want to exit?");
						color("#ffffff");
					}});
				}});
				//end upper panel
				
				//lower panel for displaying buttons
				panel(new PanelBuilder() {{
					childLayoutHorizontal();
					valignCenter();
					height("90px");
					width("260px");
					
					panel(new PanelBuilder() {{
						width("130px");
						height("90px");
						childLayoutCenter();
						
						control(new ButtonBuilder("leave_game", "Yes") {{
							visibleToMouse();
							interactOnClick("leaveGame()");
						}});
					}});
					
					panel(new PanelBuilder() {{
						width("130px");
						height("90px");
						childLayoutCenter();
						
						control(new ButtonBuilder("leave_game_cancel", "No") {{
							visibleToMouse();
							interactOnClick("closeExitGamePopup()");
						}});
					}});
				}});
				//end lower panel
			}});
		}}.registerPopup(gui.getNifty());
		
		popup = gui.getNifty().createPopup("leave_game_popup");
	}
	
	public void keyPressed(int key) {
		switch(key) {
		case Keyboard.KEY_ESCAPE: 
			if(!isShowing) {
				showPopup();
			} else {
				closePopup();
			}
			break;
		}
	}
	
	public void showPopup() {
		isShowing = true;
		gui.getNifty().showPopup(gui.getNifty().getScreen("ingameScreen"), popup.getId(), null);
	}
	
	public void closePopup() {
		isShowing = false;
		gui.getNifty().closePopup(popup.getId());
	}
	
	public boolean isShowing() {
		return isShowing;
	}
}
